﻿using Bricks.Business.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bricks.Site.Models;
using Bricks.Component.Data;
using Bricks.Site.Models.DBViewModel.Contract;
using Bricks.Component.Utils.Extensions;

namespace Bricks.Site.Web.Controllers
{
    [Export]
    public class BaklogTaskController : BaseController
    {
        [Import]
        public IBaklogTaskService BaklogTaskService { get; set; }
        [Import]
        public IFlowProcessMessageService FlowProcessMessageService { get; set; }
        [Import]
        public IAlterService AlterService { get; set; }
        [Import]
        public IAlterOrderService AlterOrderService { get; set; }
        [Import]
        public IExpenseService ExpenseService { get; set; }
        [Import]
        public ITotalMaintenanceService TotalMaintenanceService { get; set; }
        [Import]
        public IYearPlanService YearPlanService { get; set; }
        [Import]
        public IMonthPlanService MonthPlanService { get; set; }
        [Import]
        public IStageScheduleService StageScheduleService { get; set; }
        [Import]
        public ISectionSalaryService SectionSalaryService { get; set; }
        [Import]
        public IBridgeStatisticsService BridgeStatisticsService { get; set; }
        [Import]
        public ICulvertStatisticsService CulvertStatisticsService { get; set; }
        [Import]
        public ITunnelStatisticsService TunnelStatisticsService { get; set; }
        [Import]
        public ICommonContractInfoService CommonContractInfoService { get; set; }
        [Import]
        public IEngineeringImageScheduleService EngineeringImageScheduleService { get; set; }

        public ActionResult BaklogTaskList()
        {
            return View();
        }

        public ActionResult GetBaklogTaskList(int rows = 20, int page = 1)
        {
            List<BaklogTaskView> lstBaklogTaskView = BaklogTaskService.GetBaklogTaskByProcessPerson(User.Identity.Name);
            List<FlowProcessMessageView> lstFlowProcessMessageView = FlowProcessMessageService.GetFlowProcessMessageForBaklogTask(User.Identity.Name);

            List<BaklogTaskDisplayView> lstBaklogTaskDisplayView = new List<BaklogTaskDisplayView>();
            foreach (BaklogTaskView baklogTaskView in lstBaklogTaskView)
            {
                BaklogTaskDisplayView add = new BaklogTaskDisplayView();
                add.ID = baklogTaskView.ID;
                add.ProcessType = "1";
                add.AssociatedFlowCode = baklogTaskView.TaskType;
                add.AssociatedFlowNodeCode = baklogTaskView.CurrentFlowNodeCode;
                add.AssociatedProcessUserDepartment = baklogTaskView.CurrentProcessPersonDepartment;
                add.AssociatedProcessUser = baklogTaskView.CurrentProcessPerson;
                add.RecordID = baklogTaskView.RecordID;
                add.DisplayText = GetRecordBackupName(baklogTaskView.TaskType, baklogTaskView.RecordID, baklogTaskView.CurrentFlowNodeCode, baklogTaskView.CurrentProcessPersonDepartment, baklogTaskView.CurrentProcessPerson);
                add.AssociatedDate = "申请时间：" + baklogTaskView.RequestDate.ToString("yyyy-MM-dd HH:mm");
                add.DealDate = baklogTaskView.RequestDate;
                lstBaklogTaskDisplayView.Add(add);
            }
            foreach (FlowProcessMessageView flowProcessMessageView in lstFlowProcessMessageView)
            {
                BaklogTaskDisplayView add = new BaklogTaskDisplayView();
                add.ID = flowProcessMessageView.ID;
                add.ProcessType = "2";
                add.AssociatedFlowCode = flowProcessMessageView.FlowCode;
                add.AssociatedFlowNodeCode = flowProcessMessageView.FlowNodeCode;
                add.AssociatedProcessUserDepartment = flowProcessMessageView.ProcessUserDepartment;
                add.AssociatedProcessUser = flowProcessMessageView.ProcessUser;
                add.RecordID = flowProcessMessageView.RecordID;
                string displayText = GetRecordName(flowProcessMessageView.FlowCode, flowProcessMessageView.RecordID) + "由【" + UserInfoService.Users.Single(u => u.UserName == flowProcessMessageView.SponsorUser).RealName + "】传阅";
                add.DisplayText = displayText;
                add.AssociatedDate = "传阅时间：" + flowProcessMessageView.AddDate.ToString("yyyy-MM-dd HH:mm");
                add.DealDate = flowProcessMessageView.AddDate;
                lstBaklogTaskDisplayView.Add(add);
            }
            int total = lstBaklogTaskDisplayView.Count;
            var pageLstBaklogTaskDisplayView = lstBaklogTaskDisplayView.OrderBy(b => b.DisplayText).Skip(rows * (page - 1)).Take(rows);
            var result = new { total = total, rows = pageLstBaklogTaskDisplayView };
            return Json(result, "text/html");
            //return Json(lstBaklogTaskDisplayView.OrderBy(b=>b.DisplayText), JsonRequestBehavior.AllowGet);
        }

        private string GetRecordBackupName(string flowCode, string recordID, string nodeCode, string department, string userName)
        {
            string recordName = string.Empty;
            switch (flowCode)
            {
                case "AlterOne":
                case "AlterTwo":
                case "AlterThree":
                    AlterView alterView = AlterService.GetAlterBackupByCondition(recordID, nodeCode, department, userName);
                    if (alterView != null)
                    {
                        recordName = "变更申请【" + alterView.AlterName + "】";
                    }
                    break;
                case "AlterOrderOne":
                    AlterOrderView alterOrderView = AlterOrderService.GetAlterOrderBackupByCondition(recordID, nodeCode, department, userName);
                    if (alterOrderView != null)
                    {
                        recordName = "变更令申请【" + alterOrderView.AlterOrderName + "】";
                    }
                    break;
                case "Expense":
                    ExpenseView expenseView = ExpenseService.GetExpenseBackupByCondition(recordID, nodeCode, department, userName);
                    if (expenseView != null)
                    {
                        recordName = "合同【" + GetContractName(expenseView) + "】的第【" + expenseView.ContractStages + "】期付款申请";
                    }
                    break;
                case "TotalMaintenance":
                    TotalMaintenanceView totalMaintenanceView = TotalMaintenanceService.GetTotalMaintenanceByID(recordID.ToGuid());
                    if (totalMaintenanceView != null)
                    {
                        recordName = "合同【" + GetContractName(totalMaintenanceView.ContractID) + "】的总量维护申请";
                    }
                    break;
                case "YearPlan":
                    YearPlanView yearPlanView = YearPlanService.GetYearPlanByID(recordID.ToGuid());
                    if (yearPlanView != null)
                    {
                        recordName = "合同【" + GetContractName(yearPlanView.ContractID) + "】在统计周期【" + yearPlanView.StatisticalCycle + "年】的年度计划申请";
                    }
                    break;
                case "MonthPlan":
                    MonthPlanView monthPlanView = MonthPlanService.GetMonthPlanByID(recordID.ToGuid());
                    if (monthPlanView != null)
                    {
                        recordName = "合同【" + GetContractName(monthPlanView.ContractID) + "】在统计周期【" + monthPlanView.StatisticalCycle + "】的月度计划申请";
                    }
                    break;
                case "EngineeringImageSchedule":
                    EngineeringImageScheduleView engineeringImageScheduleView = EngineeringImageScheduleService.GetEngineeringImageScheduleByID(recordID.ToGuid());
                    if (engineeringImageScheduleView != null)
                    {
                        recordName = "合同【" + GetContractName(engineeringImageScheduleView.ContractID) + "】在统计周期【" + engineeringImageScheduleView.StatisticalCycle + "】的工程形象进度申请";
                    }
                    break;
                case "StageSchedule":
                    StageScheduleView stageScheduleView = StageScheduleService.GetStageScheduleByID(recordID.ToGuid());
                    if (stageScheduleView != null)
                    {
                        recordName = "合同【" + GetContractName(stageScheduleView.ContractID) + "】在【" + stageScheduleView.Year + "年" + stageScheduleView.Month + "月" + stageScheduleView.StatisticalCycle + "】的旬计划申请";
                    }
                    break;
                case "SectionSalary":
                    SectionSalaryView sectionSalaryView = SectionSalaryService.GetSectionSalaryByID(recordID.ToGuid());
                    if (sectionSalaryView != null)
                    {
                        recordName = "合同【" + GetContractName(sectionSalaryView.ContractID) + "】在【" + sectionSalaryView.Year + "】年【" + sectionSalaryView.Month + "】农民工工资申请";
                    }
                    break;
                //桥梁统计
                case "BridgeStatistics":
                    BridgeStatisticsView bridgeStatisticsView = BridgeStatisticsService.GetBridge(recordID.ToGuid());
                    if (bridgeStatisticsView != null)
                    {
                        recordName = "合同【" + GetContractName(bridgeStatisticsView.ContractID) + "】在统计周期【" + bridgeStatisticsView.StatisticalCycle + "】的桥梁统计申请";
                    }
                    break;
                //涵洞统计
                case "CulvertStatistics":
                    CulvertStatisticsView culvertStatisticsView = CulvertStatisticsService.GetCulvert(recordID.ToGuid());
                    if (culvertStatisticsView != null)
                    {
                        recordName = "合同【" + GetContractName(culvertStatisticsView.ContractID) + "】在统计周期【" + culvertStatisticsView.StatisticalCycle + "】的涵洞统计申请";
                    }
                    break;
                //隧道统计
                case "TunnelStatistics":
                    TunnelStatisticsView tunnelStatisticsView = TunnelStatisticsService.GetTunnel(recordID.ToGuid());
                    if (tunnelStatisticsView != null)
                    {
                        recordName = "合同【" + GetContractName(tunnelStatisticsView.ContractID) + "】在统计周期【" + tunnelStatisticsView.StatisticalCycle + "】的隧道统计申请";
                    }
                    break;
            }
            return recordName;
        }

        private string GetContractName(ExpenseView expenseView)
        {
            string contractName = string.Empty;
            if (expenseView.ContractSource == "Construction")
            {
                string sql = "select * from V_ContractInfoes where ID='" + expenseView.ContractID + "'";
                var listView = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);
                if (listView != null && listView.Count == 1)
                {
                    contractName = listView[0].ContractName;
                }
            }
            else
            {
                contractName = CommonContractInfoService.GetCommonContractName(expenseView.ContractID);
            }
            return contractName;
        }

        private string GetContractName(Guid contractID)
        {
            string contractName = string.Empty;
            string sql = "select * from V_ContractInfoes where ID='" + contractID + "'";
            var listView = DbSqlHelper<VMContractInfo>.SqlQueryCommon(sql);
            if (listView != null && listView.Count == 1)
            {
                contractName = listView[0].ContractName;
            }
            return contractName;
        }

        private string GetRecordName(string flowCode, string recordID)
        {
            string recordName = string.Empty;
            switch (flowCode)
            {
                case "AlterOne":
                case "AlterTwo":
                case "AlterThree":
                    AlterView alterView = AlterService.GetAlterByID(recordID);
                    if (alterView != null)
                    {
                        recordName = "变更申请【" + alterView.AlterName + "】";
                    }
                    break;
                case "AlterOrderOne":
                    AlterOrderView alterOrderView = AlterOrderService.GetAlterOrderByID(recordID);
                    if (alterOrderView != null)
                    {
                        recordName = "变更令申请【" + alterOrderView.AlterOrderName + "】";
                    }
                    break;
                case "Expense":
                    ExpenseView expenseView = ExpenseService.GetExpenseByID(recordID);
                    if (expenseView != null)
                    {
                        recordName = "合同【" + GetContractName(expenseView) + "】的第【" + expenseView.ContractStages + "】期付款申请";
                    }
                    break;
                case "TotalMaintenance":
                    TotalMaintenanceView totalMaintenanceView = TotalMaintenanceService.GetTotalMaintenanceByID(recordID.ToGuid());
                    if (totalMaintenanceView != null)
                    {
                        recordName = "合同【" + GetContractName(totalMaintenanceView.ContractID) + "】的总量维护申请";
                    }
                    break;
                case "YearPlan":
                    YearPlanView yearPlanView = YearPlanService.GetYearPlanByID(recordID.ToGuid());
                    if (yearPlanView != null)
                    {
                        recordName = "合同【" + GetContractName(yearPlanView.ContractID) + "】在统计周期【" + yearPlanView.StatisticalCycle + "年】的年度计划申请";
                    }
                    break;
                case "MonthPlan":
                    MonthPlanView monthPlanView = MonthPlanService.GetMonthPlanByID(recordID.ToGuid());
                    if (monthPlanView != null)
                    {
                        recordName = "合同【" + GetContractName(monthPlanView.ContractID) + "】在统计周期【" + monthPlanView.StatisticalCycle + "】的月度计划申请";
                    }
                    break;
                case "EngineeringImageSchedule":
                    EngineeringImageScheduleView engineeringImageScheduleView = EngineeringImageScheduleService.GetEngineeringImageScheduleByID(recordID.ToGuid());
                    if (engineeringImageScheduleView != null)
                    {
                        recordName = "合同【" + GetContractName(engineeringImageScheduleView.ContractID) + "】在统计周期【" + engineeringImageScheduleView.StatisticalCycle + "】的工程形象进度申请";
                    }
                    break;
                case "StageSchedule":
                    StageScheduleView stageScheduleView = StageScheduleService.GetStageScheduleByID(recordID.ToGuid());
                    if (stageScheduleView != null)
                    {
                        recordName = "合同【" + GetContractName(stageScheduleView.ContractID) + "】在【" + stageScheduleView.Year + "年" + stageScheduleView.Month + "月" + stageScheduleView.StatisticalCycle + "】的旬计划申请";
                    }
                    break;
                case "SectionSalary":
                    SectionSalaryView sectionSalaryView = SectionSalaryService.GetSectionSalaryByID(recordID.ToGuid());
                    if (sectionSalaryView != null)
                    {
                        recordName = "合同【" + GetContractName(sectionSalaryView.ContractID) + "】在【" + sectionSalaryView.Year + "】年【" + sectionSalaryView.Month + "】农民工工资申请";
                    }
                    break;
                //桥梁统计
                case "BridgeStatistics":
                    BridgeStatisticsView bridgeStatisticsView = BridgeStatisticsService.GetBridge(recordID.ToGuid());
                    if (bridgeStatisticsView != null)
                    {
                        recordName = "合同【" + GetContractName(bridgeStatisticsView.ContractID) + "】在统计周期【" + bridgeStatisticsView.StatisticalCycle + "】的桥梁统计申请";
                    }
                    break;
                //涵洞统计
                case "CulvertStatistics":
                    CulvertStatisticsView culvertStatisticsView = CulvertStatisticsService.GetCulvert(recordID.ToGuid());
                    if (culvertStatisticsView != null)
                    {
                        recordName = "合同【" + GetContractName(culvertStatisticsView.ContractID) + "】在统计周期【" + culvertStatisticsView.StatisticalCycle + "】的涵洞统计申请";
                    }
                    break;
                //隧道统计
                case "TunnelStatistics":
                    TunnelStatisticsView tunnelStatisticsView = TunnelStatisticsService.GetTunnel(recordID.ToGuid());
                    if (tunnelStatisticsView != null)
                    {
                        recordName = "合同【" + GetContractName(tunnelStatisticsView.ContractID) + "】在统计周期【" + tunnelStatisticsView.StatisticalCycle + "】的隧道统计申请";
                    }
                    break;
            }
            return recordName;
        }
    }
}